package com.mx.kn.cstf2.actions.crud.transition;

import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import kn.cstf2.transition.Bean.JoinPersonTransitionBean;
import kn.cstf2.transition.Bean.PersonalBean;
import kn.cstf2.transition.Bean.TransitionBean;

import com.mx.kn.cstf2.dto.UsuarioDto;
import com.mx.kn.cstf2.service.TransitionService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@Component("deletePersonOptionTransitionAction")
@Scope(value="session")
public class DeletePersonOptionTransitionAction extends ActionSupport{

	private String id;
	private String result;
	 @Autowired TransitionService transitionService;
	
	public String deletePerson(){
		ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesion2 = contexto.getSession();
		
		JoinPersonTransitionBean idperson = new JoinPersonTransitionBean();
        idperson.setIdperson(Integer.parseInt(id));
        try {
			transitionService.deleteRelationJoinusertransitionByIdPerson(Integer.parseInt(id), ((TransitionBean)sesion2.get("Knkey")).getId());
			  int veryf = transitionService.verificarCountJoinPersontransition(idperson);
	            if (veryf == 0) {
	                PersonalBean pb = new PersonalBean();
	                pb.setId(Integer.parseInt(id));
	                transitionService.deletePersonalById(pb);
	            } else {
	            
	            }
	        PersonalBean personalBeanEliminado = transitionService.getDataPersonalById(Integer.parseInt(id));
	        String nombreEliminado = personalBeanEliminado.getName();    
	        String comment = "Se Elimino una persona de la base de datos:" + " - " + nombreEliminado;
//			String insert = this.getUsuarioT().getUsername() + " " + new Date() + " " + comment + "\n";
//			this.insertLog(insert);   
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        result = "OK";
        if (((UsuarioDto) sesion2.get("usuarioSession")).getUsername().trim().equals("STK")) {
            return "knkeylist";
        } else {
            return SUCCESS;
        }
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
    
	
	
}
